package com.spzx.system.test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtExample {
    public static void main(String[] args) {
        // 生成令牌
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("888")//唯一ID
                .setSubject("atguigu")//接受用户
                .setIssuedAt(new Date())//签发时间
                .signWith(SignatureAlgorithm.HS512, "1234");//签名算法、和秘钥
        // secret key byte array cannot be null or empty.
        // key不能太短 最短四个字符！！！
        String token = jwtBuilder.compact();
        System.out.println("生成令牌：【" + token + "】");
		// 头信息  载体   令牌加密
        //生成令牌：【eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiJ6aGFvIiwiaWF0IjoxNzE5NDk1NzU5fQ.J8YK1HdB2CSN5BpMIQaQXtFKWhymgZ8cv1gpbvBpUDSzwFfw0tXllK8WOcxRcxwAVuRaO1ULdgSc4Sq2t5enyQ】


        // 解析令牌
        //String token = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiJ6aGFvIiwiaWF0IjoxNzE5NDk1NzU5fQ.J8YK1HdB2CSN5BpMIQaQXtFKWhymgZ8cv1gpbvBpUDSzwFfw0tXllK8WOcxRcxwAVuRaO1ULdgSc4Sq2t5enyQ";

        Claims claims = (Claims) Jwts.parser()
                .setSigningKey("1234")
                .parse(token)
                .getBody();
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getIssuedAt());
        System.out.println("解析令牌：【" + claims + "】");

    }
}